package com.asiainfo.dacp.datastash.broker.core.vo;

import com.asiainfo.dacp.core.BeanFactory;
import com.asiainfo.dacp.datastash.broker.core.enums.UnitBatchRunConstraintEnum;
import com.asiainfo.dacp.datastash.broker.core.enums.UnitStepTypeEnum;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogVo;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogger;
import com.asiainfo.dacp.datastash.domain.unit.models.DataStashUnitStep;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 单元在broker传递的值对象
 *
 * @author MeiKefu
 * @date 2017-11-20
 */
@Data
public class StashUnitDefine {

    private String unitCode = "unknown";

    private String label;

    private String unitType;

    private String workgroup;

    private String state;//单元状态，开发，在用，下线

    private String batchType; //批次类型

    private int batchOffset;

    private int batchStep;

//    private String batchNo; //批次

    private int unitPriority;

    private String runConstraint; //启动约束，1，顺序运行，2，并发运行

    private String tenantId;

    private int tenantPriority;
    
    private StashUnitSchedule stashUnitSched;//调度策略，批次调度，准实时文件，流启停

    private List<DataStashUnitStep> unitSteps;

    private List<DataStashUnitStep> rootSteps;

    private Map<String,DataStashUnitStep> stepIndex;

    public static StashUnitDefine NULL = new StashUnitDefine();

    private String clzName = this.getClass().getSimpleName();
    
    public DataStashUnitStep getStepByInx(String inx){
    	if(stepIndex == null || stepIndex.size() == 0){
    		return null;	
    	}
    	return this.stepIndex.get(inx);
    }
    
    /**
     * 是否顺序执行
     * @return
     */
    @JsonIgnore
    public boolean isSequential(){
        return !UnitBatchRunConstraintEnum.Parallel.value().equals(runConstraint);
    }

    public void transformUnitSched(List<DataStashUnitStep> dataStashUnitSteps){
        StashTraceLogger tracelog = BeanFactory.getBean(StashTraceLogger.class);
        if(dataStashUnitSteps.size()>0){
            for (DataStashUnitStep dataStashUnitStep : dataStashUnitSteps){
                if(UnitStepTypeEnum.Schedule.value().equals(dataStashUnitStep.getStepDefine().getStepType())){
                    if(dataStashUnitStep.getStepCfgValMap()==null|| dataStashUnitStep.getStepCfgValMap().size()==0){
                        tracelog.error(StashTraceLogVo.buildStashTraceLogVo( clzName,"transformUnitSched:error","暂时为支持依赖的类型",unitCode,null,null));
                    }else{
                        //TODO 应该把step解析成参数类的形式
                        stashUnitSched = new StashUnitSchedule();
                        String schedType = (String)dataStashUnitStep.getStepCfgValMap().get("schedType");
                        String dsName = (String)dataStashUnitStep.getStepCfgValMap().get("dsName");
                        String stateExpr = (String) dataStashUnitStep.getStepCfgValMap().get("stateExpr");
                        stashUnitSched.setSchedType(schedType);
                        stashUnitSched.setDsName(dsName);
                        stashUnitSched.setStateExpr(stateExpr);
                    }
                    break;
                }
            }
        }
    }
}
